package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements Operation {
    public void work(BookList list) {
        System.out.println("删除图书...");
        System.out.println("请输入要删除的书名：");
        Scanner sc = new Scanner(System.in);
        String delBook = sc.nextLine();
        int currentsize = list.getSizeUsed();
        for (int i = 0; i < currentsize; i++) {
            if (delBook.equals(list.getBook(i).getBookName())) {
                //若是最后一本书
                if (i == currentsize - 1) {
                    list.setSizeUsed(currentsize-1);
                    list.setBookList(i,null);
                    System.out.println("删除成功!!!");
                    return;
                }
                //删除任意一本书
                for (int j = 1; j < currentsize-i; j++) {
                    Book nextBook = list.getBook(i+1);
                    list.setBookList(i,nextBook);
                    i++;
                }
                list.setBookList(i,null);
                list.setSizeUsed(currentsize-1);
                System.out.println("删除成功!!!");
                return;
            }
        }
        System.out.println("没有找到要删除的这本书!!!");
    }
}
